package com.pay.ioopos;

import static com.pay.ioopos.App.SERVER_TYPE_A_PAY;
import static com.pay.ioopos.App.SERVER_TYPE_C_PAY;
import static com.pay.ioopos.App.SERVER_TYPE_I_PAY;
import static com.pay.ioopos.AppUtils.serverType;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.pay.ioopos.support.scan.weixin.MyWxPayFace;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class ComUtils {

    public static String sha256(String str) {
        MessageDigest messageDigest;
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            return byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String byte2Hex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        String temp;
        for (byte aByte : bytes) {
            temp = Integer.toHexString(aByte & 0xFF);
            if (temp.length() == 1) {
                sb.append("0");
            }
            sb.append(temp);
        }
        return sb.toString();
    }

    public static boolean isApkInDebug(Context context) {
        try {
            ApplicationInfo info = context.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {
            return false;
        }
    }

    @SuppressLint("MissingPermission")
    public static String sn() {
        //return "SP3082003231580";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return android.os.Build.getSerial();
        } else {
            return Build.SERIAL;
        }

    }

    public static String imei(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            if (ctx.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return null;
            }
            return tm.getDeviceId();
        }
        return null;
    }

    public static String appVersion(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
        } catch (PackageManager.NameNotFoundException e) {

        }
        if (null != packInfo) {
            return packInfo.versionName;
        }
        return "0.0.0";
    }

    public static String appVersionName(Context context) {
        int serverType = serverType();
        String tag = "";
        switch (serverType) {
            case SERVER_TYPE_I_PAY:
                tag = MyWxPayFace.IS_OFFLINE ? " f-pay":" o-pay";
                break;
            case SERVER_TYPE_C_PAY:
                tag = " c-pay";
                break;
            case SERVER_TYPE_A_PAY:
                tag = " a-pay";
                break;
        }
        return appVersion(context) + tag;
    }
}
